import { reactive, ref } from 'vue'
import defaultAvatar from '@/assets/image/introduce/default_avatar.png'
import { IntroduceTypes, DetailType, MsgType, TagsType, EvaluateTabs } from '../utils/types'
import { UserTypesEm } from '@/store/enums'
import store from '@/store'
import { EvaTabsEm } from '../utils/enums'

const useIntroduce = (userId: string, userType: string): any => {
  // 基本简介信息
  const basicInfo = reactive<DetailType>({
    avatar: defaultAvatar,
    name: '',
    phone: '',
    validate: false,
    commentNum: 0
  })

  // 简介详情
  const infoList = ref<MsgType[]>([])

  // 车辆标签
  const carLabel = ref<string[]>([])

  // 评价标签
  const evaluateLabelList = ref<string[]>([])

  // 司机/货主 评价 tab栏数据
  const evaluateTabs = reactive<EvaluateTabs>({
    tabs: {
      all: {
        description: '全部',
        value: EvaTabsEm.ALL.value,
        list: [],
        num: 0,
        key: 'all'
      },
      praiseTags: {
        description: '好评',
        value: EvaTabsEm.GOODS.value,
        list: [],
        num: 0,
        key: 'praiseTags'
      },
      middleTags: {
        description: '中评',
        value: EvaTabsEm.MIDDLE.value,
        list: [],
        num: 0,
        key: 'middleTags'
      },
      negativeTags: {
        description: '差评',
        value: EvaTabsEm.BAD.value,
        list: [],
        num: 0,
        key: 'negativeTags'
      }
    },
    currentTab: 'all',
    praiseRate: 0
  })

  // 构建司机/货主 tabs
  const buildTabsData = (data: IntroduceTypes): void => {
    evaluateTabs.tabs.all.list = []
    evaluateTabs.praiseRate = data.praiseRate
    evaluateTabs.tabs.praiseTags.num = data.praiseNum
    evaluateTabs.tabs.middleTags.num = data.middleNum
    evaluateTabs.tabs.negativeTags.num = data.negativeNum
    evaluateTabs.tabs.all.num = data.commentNum
    for (const item in evaluateTabs.tabs) {
      // 遍历数据源赋值
      if (item === 'all') {
        evaluateTabs.tabs.all.list = data.allTags || []
      } else {
        if (data[item] && data[item].length) {
          evaluateTabs.tabs[item].list = data[item]
        }
      }
    }
  }

  // 构建评价标签数据
  const buildLabelData = (praiseTags: TagsType[]): void => {
    const list = praiseTags?.slice(0, 3) ?? []
    list.forEach((item: TagsType) => {
      evaluateLabelList.value.push(`${item.tagName}(${item.tagNum || 0})`)
    })
  }

  // 构建简介车辆标签
  const buildCarLabelData = (data: IntroduceTypes): void => {
    carLabel.value = [
      // data.plateNo,
      // `${(Number(data.vehicleLong) / 1000).toFixed(1)}米${data.vehicleType || '-'}`,
      // `核定载重${data.quality || 0}吨`
    ]
  }

  // 构建简介描述
  const buildMemoData = (data: IntroduceTypes): void => {
    basicInfo.name = data.userName
    basicInfo.phone = data.contactPhone
    basicInfo.validate = data.authStatus
    basicInfo.commentNum = data.commentNum
  }

  // 构建简介数据list
  const buldListData = (data: IntroduceTypes): void => {
    let grade = String(data.grade || '0.0')
    grade = String(grade).slice(0, 3)
    grade = grade.length < 3 ? `${grade}.0` : grade
    if (Number(userType) === UserTypesEm.CARGOOWNER.value) {
      // 货主
      infoList.value = [
        {
          title: '联系地址',
          description: data.registerAddress
        },
        {
          title: '注册日期',
          description: data.registerTime
        },
        {
          title: '评分',
          description: grade,
          color: '#FC9E29'
        },
        {
          title: '交易数',
          description: data.tradeNum
        }
      ]
    } else if (Number(userType) === UserTypesEm.DRIVER.value) {
      // 司机
      infoList.value = [
        {
          title: '关联承运商',
          description: data.carrierName
        },
        {
          title: '注册日期',
          description: data.registerTime
        },
        {
          title: '车牌号',
          description: data.plateNo
        },
        {
          title: '车长车型',
          description: `${(Number(data.vehicleLong) / 1000).toFixed(1)}米${data.vehicleType || '-'}`
        },
        {
          title: '核定载重',
          description: `${data.quality || 0}吨`
        },
        {
          title: '司机评价',
          description: grade,
          color: '#FC9E29'
        },
        {
          title: '交易数',
          description: data.tradeNum
        }
      ]
      buildCarLabelData(data)
    } else if (Number(userType) === UserTypesEm.CARRIER.value) {
      //承运人
      infoList.value = [
        {
          title: '注册日期',
          description: data.registerTime
        },

        {
          title: '承运商评价',
          description: grade,
          color: '#FC9E29'
        },
        {
          title: '交易数',
          description: data.tradeNum
        }
      ]
    }
  }
  // 构建简介数据
  const buildBasicData = (data: IntroduceTypes) => {
    buildMemoData(data)
    buildLabelData(data?.praiseTags ?? [])
    buldListData(data)
  }
  // 获取简介
  const handleGetIntroduce = async (): Promise<void> => {
    const result = await store.dispatch('introduce/handleTransportBillCommentPersonSynopsis', {
      userId,
      userType
    })
    if (result) {
      buildBasicData(result)
      buildTabsData(result)
    }
  }

  return {
    basicInfo,
    carLabel,
    infoList,
    evaluateLabelList,
    evaluateTabs,
    handleGetIntroduce
  }
}
export default useIntroduce
